package com.mobile.collect.beans;

import android.view.View;
import android.widget.TextView;

import com.mobile.collect.R;
import com.mobile.collect.view.InputDialog;
import com.mobile.collect.view.TypeDialog;

/**
 * @link R.laout.
 */
public class PointViewInfo implements View.OnClickListener {
    private Long updateId;
    private TextView childTypeTv;
    private TextView dimenTv;
    private TextView numberTv;
    private View mView;
    private PointViewInfo aboutPointView;

    String type;
    String typeEn;
    String childTypeEn;
    String childType;
    int count;
    AddPointListener addPointListener;

    public PointViewInfo(Long updateId, AddPointListener addPointListener) {
        this.updateId = updateId;
        this.addPointListener = addPointListener;
    }

    public PointViewInfo(AddPointListener addPointListener) {
        this.addPointListener = addPointListener;
    }


    public View getmView() {
        return mView;
    }

    public void setmView(View mView, String childTypeEn, String dimen, int count) {
        this.mView = mView;
        childTypeTv = mView.findViewById(R.id.childTypeTv);
        dimenTv = mView.findViewById(R.id.dimenTv);
        numberTv = mView.findViewById(R.id.numberTv);
        childTypeTv.setOnClickListener(this);
        dimenTv.setOnClickListener(this);
        numberTv.setOnClickListener(this);
        mView.findViewById(R.id.addIv).setOnClickListener(this);
        mView.findViewById(R.id.delIv).setOnClickListener(this);

        childTypeTv.setText(childTypeEn);
        dimenTv.setText(dimen);
        numberTv.setText(String.valueOf(count));

    }

    public TextView getChildTypeTv() {
        return childTypeTv;
    }

    public void setChildTypeTv(TextView childTypeTv) {
        this.childTypeTv = childTypeTv;
    }

    public TextView getDimenTv() {
        return dimenTv;
    }

    public void setDimenTv(TextView dimenTv) {
        this.dimenTv = dimenTv;
    }

    public TextView getNumberTv() {
        return numberTv;
    }

    public void setNumberTv(TextView numberTv) {
        this.numberTv = numberTv;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.childTypeTv:
                showTypeDialog();
                break;
            case R.id.dimenTv:
                showDimenDialog();
                break;
            case R.id.numberTv:
                showCountDialog();
                break;
            case R.id.addIv:
                addPointListener.toAddNewPoint(this);
                break;
            case R.id.delIv:
                addPointListener.toDelPoint(this);
                break;
        }
    }


    private void showTypeDialog() {
        TypeDialog numberPop = new TypeDialog(mView.getContext(), "组件子类型", new TypeDialog.TypeCompleteListener() {
            @Override
            public void onInputComplete(String value) {

            }

            @Override
            public void setType(TypeInfo childTypeInfo) {
                type = childTypeInfo.getTypeName();
                typeEn = childTypeInfo.getTypeCode();
                childTypeEn = childTypeInfo.getChildCode();
                childType = childTypeInfo.getChildName();
                count = 1;
                childTypeTv.setText(childTypeEn);
                toAutoFalan(childTypeEn);
            }
        });
        numberPop.show();
    }

    private void showDimenDialog() {
        InputDialog numberPop = new InputDialog(mView.getContext(), "尺寸", new InputDialog.InputCompleteListener() {
            @Override
            public void onInputComplete(String value) {
//                info.setDimen(value);
                if (aboutPointView != null) {
                    aboutPointView.getDimenTv().setText(value);
                }
                dimenTv.setText(value);
            }
        }, 0);
        numberPop.disPlay(InputDialog.broad6, true);
    }

    public PointViewInfo getAboutPointView() {
        return aboutPointView;
    }

    public void setAboutPointView(PointViewInfo aboutPointView) {
        this.aboutPointView = aboutPointView;
    }

    private void showCountDialog() {
        InputDialog numberPop = new InputDialog(mView.getContext(), "数量", new InputDialog.InputCompleteListener() {
            @Override
            public void onInputComplete(String value) {
                try {
                    numberTv.setText(value);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

            }
        }, 0);
        numberPop.disPlay(InputDialog.broad1, true);
    }

    public Long getUpdateId() {
        return updateId;
    }

    public void setUpdateId(Long updateId) {
        this.updateId = updateId;
    }

    public void setmView(View mView) {
        this.mView = mView;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getTypeEn() {
        return typeEn;
    }

    public void setTypeEn(String typeEn) {
        this.typeEn = typeEn;
    }

    public String getChildTypeEn() {
        return childTypeEn;
    }

    public void setChildTypeEn(String childTypeEn) {
        this.childTypeEn = childTypeEn;
    }

    public String getChildType() {
        return childType;
    }

    public void setChildType(String childType) {
        this.childType = childType;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    private void toAutoFalan(String childTypeEn) {
        switch (childTypeEn) {
            case "GV":
            case "CV":
            case "BUV":
            case "GLV":
            case "BV": {
                if (aboutPointView == null) {
                    aboutPointView = addPointListener.toAddFanLan(this);
                    aboutPointView.getDimenTv().setText(dimenTv.getText().toString());
                }
            }
        }
    }

    public interface AddPointListener {
        PointViewInfo toAddFanLan(PointViewInfo pointViewInfo);
        PointViewInfo toAddNewPoint(PointViewInfo pointViewInfo);
        PointViewInfo toDelPoint(PointViewInfo pointViewInfo);
    }

}
